package cn.wjx.taobaounion.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.model.domain.OnSaleContent;
import cn.wjx.taobaounion.utils.URLUtils;

/**
 * @author WuChangJian
 * @date 2020/6/26 12:07
 */
public class OnSalePageAdapter extends RecyclerView.Adapter<OnSalePageAdapter.InnerHolder> {
    List<OnSaleContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mDatas = new ArrayList<>();
    private OnSalePageItemClickListener mListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_on_sale_content, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mDatas.get(position));
        holder.itemView.setOnClickListener(v-> {
            if (mListener != null) {
                mListener.onSaleItemClick(mDatas.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setData(OnSaleContent content) {
        List<OnSaleContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> data = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();
    }

    public void setOnSalePageItemClickListener(OnSalePageItemClickListener listener) {
        this.mListener = listener;
    }

    public void loaderData(OnSaleContent content) {
        List<OnSaleContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> data = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        int oldSize = mDatas.size();
        mDatas.addAll(data);
        notifyItemChanged(oldSize, data.size());
    }

    public interface OnSalePageItemClickListener {
        void onSaleItemClick(OnSaleContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.on_sale_cover)
        public ImageView goodsCover;
        @BindView(R.id.on_sale_origin_price_tv)
        public TextView originPrice;
        @BindView(R.id.on_sale_off_price_tv)
        public TextView discountPrice;
        @BindView(R.id.on_sale_content_title_tv)
        public TextView titleTv;



        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(OnSaleContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean data) {
            Context context = itemView.getContext();
            titleTv.setText(data.getTitle());
            String coverPath = URLUtils.getTicketPath(data.getPict_url());
            Glide.with(context).load(coverPath).into(goodsCover);
            originPrice.setText(String.format(context.getString(R.string.text_sale_origin_price), data.getZk_final_price()));
            originPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            int coupAmount = data.getCoupon_amount();
            float finalPrice = Float.parseFloat(data.getZk_final_price()) - coupAmount;
            discountPrice.setText("券后价：" + String.format("%.2f", finalPrice));

        }
    }
}
